package com.briup.app02.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.app02.bean.BaseFile;
import com.briup.app02.bean.BaseFileExample;
import com.briup.app02.dao.BaseFileMapper;
import com.briup.app02.service.IBaseFileService;
import com.briup.app02.utils.CustomerException;

@Service
public class BaseFileServiceImpl implements IBaseFileService {
	@Autowired
	private BaseFileMapper basefileMapper;
	
	@Override
	public List<BaseFile> query(String filename) {
		// 模板查询
		BaseFileExample example = new BaseFileExample();
		if(filename!=null){
			example.createCriteria().andFileNameLike("%"+filename+"%");
		}
		List<BaseFile> list = basefileMapper.selectByExample(example);
		return list;
	}

	@Override
	public void saveOrUpdate(BaseFile basefile) throws CustomerException{
		if(basefile.getId() == null) {
			basefileMapper.updateByPrimaryKey(basefile);
		} else {
			BaseFileExample example = new BaseFileExample();
			example.createCriteria().andFileNameEqualTo(basefile.getFileName());
			List<BaseFile> list = basefileMapper.selectByExample(example);;
			if(list.size()>0) {
				throw new CustomerException("同名的分类已存在");
			}
			basefileMapper.insert(basefile);
		}
	}

	@Override
	public void deleteById(String id) throws CustomerException{
		BaseFile baseFile = basefileMapper.selectByPrimaryKey(id);
		if(baseFile == null) {
			throw new CustomerException("要删除的分类信息不存在");
		}
		basefileMapper.deleteByPrimaryKey(id);
	}
}
